#include "cbutton.h"

/**
 * CButton - constructor, initialization
 * @param text
 */
CButton::CButton(string text) {
  this->init();
  this->text = text;
}
//---------------------------------------------------------------------------

/**
 * getText - returns text
 * @return text
 */
string CButton::getText() const {
  return text;
}
//---------------------------------------------------------------------------

/**
 * setText - sets text
 * @param text
 */
void CButton::setText(string text) {
  this->text = text;
}
//---------------------------------------------------------------------------

/**
 * drawSelf - draws button
 * @param rendererPtr
 */
void CButton::drawSelf(CGuiRenderer* rendererPtr) {

  // Button dimensions without border
  unsigned short w(width - (style.getBorder().getSize() << 1));
  unsigned short h(height - (style.getBorder().getSize() << 1));

  // Buttom text dimensions
  unsigned short textW(rendererPtr->calculateTTFWidth(fontId, text));
  unsigned short textH(rendererPtr->calculateTTFHeight(fontId));

  // If text fits in button, draw it
  string tmp(text);
  if (textH <= h && this->fitTextToWidth(tmp, textW, w, rendererPtr)) {
    int l(x + ((width - textW) >> 1)), t(y + ((height - textH) >> 1));
    rendererPtr->drawTextRect(l, t, l + textW, t + textH, fontId, tmp);
  }
}
//---------------------------------------------------------------------------
